/*
  "name": "大山后台管理系统",
  "version": "1.0",
  "description": "大山中后台管理系统",
  "author": "大山",
  "date": "2023-04-04",
  "license": "MIT",
*/

// 请求express
const express = require('express');
// 请求body-parser，express听说最新版本没有body-parser，请另行安装
const bodyParser = require('body-parser');
// api
const userApi = require('./apiRouter/user.js');
const loginApi = require('./apiRouter/login.js');
const loginInfoApi = require('./apiRouter/loginInfo.js');
const menuApi = require('./apiRouter/menu.js');
const deptApi = require('./apiRouter/dept.js');
const publicApi = require('./apiRouter/public.js');
const operaInfoApi = require('./apiRouter/operaInfo.js');
const noticeApi = require('./apiRouter/notice.js');
const roleApi = require('./apiRouter/role.js');
const dictApi = require('./apiRouter/dict.js');
const dictDataApi = require('./apiRouter/dictData.js');

const app = express();
// node.js依赖express解析post请求四种数据格式文档：https://www.jianshu.com/p/606802e40fd5
app.use(express.urlencoded({ extended: false }));
// parse application/x-www-form-urlencoded 默认格式
app.use(bodyParser.urlencoded({extended: true }));
// parse application/json  json格式 post接收json格式，这里需要格式接收
app.use(bodyParser.json());

//跨越解决
app.all('*', function (req, res, next, error) {
  // 本地项目访问本地nodeJs服务端项目，本地项目使用前端代理，以下方法无效
  
  res.header('Access-Control-Allow-Credentials', true)
  // 这里获取 origin 请求头 而不是用 *
  res.header('Access-Control-Allow-Origin', req.headers.origin || '*')
  res.header("Access-Control-Allow-Headers", "*");
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  next()
});

app.get('/', (req, res) => res.send('hello world!'));

userApi(app);
loginApi(app);
loginInfoApi(app);
menuApi(app);
publicApi(app);
operaInfoApi(app);
noticeApi(app);
roleApi(app);
dictApi(app);
dictDataApi(app);
deptApi(app);

app.listen(3000, ()=>{
    console.log('接口服务器启动,3000...')
})
